﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AbraReportManagement.SygmaNoteReport
{
    class ReportEngine
    {
        //TODO:

        //NrWykazu;NrSklepu;NrRef.;DataZest;Numer umowy;Nazw. klienta;Nr.Autor.;Nr. Ref.;Dat oferty;Wys kredyt;Udz sklepu;Debet;Kredyt

        private Data readData = new Data();

        public static List<string> DecodedStringList(List<string> _codedData)
        {
            List<string> decodedData = new List<string>();

            foreach (var q in _codedData)
            {
                byte[] data = Convert.FromBase64String(q);
                decodedData.Add(Encoding.UTF8.GetString(data));
            }
            Console.WriteLine("Decoding SUCCESS!!!");
            return decodedData;
        }

        public static List<string> CodedStringList(string[] _readedFile)
        {
            List<string> encodedData = new List<string>();

            for (int i = 0; i < _readedFile.Length; i++)
            {
                if (_readedFile[i] == "Content-Transfer-Encoding: base64")
                {
                    StringBuilder sb = new StringBuilder();
                    string beginSection = _readedFile[i - 2];
                    string endSection = beginSection + "--";
                    int j = 1;

                    while (_readedFile[i + j] != endSection)
                    {
                        sb.Append(_readedFile[i + j]);
                        j++;
                    }
                    encodedData.Add(sb.ToString());
                    i = i + j;
                    j = 0;
                }

            }
            Console.WriteLine("Reading coded data SUCCESS!!!");
            return encodedData;
        }

        public static string[] ReadFile(string selectedPath)
        {


            string readedRecord = "";
            List<string> encodedData = new List<string>();
            List<string> decodedData = new List<string>();

            string path = "";
            if (selectedPath == "1")
            {
                path = "c:\\poczta\\noty\\Mail\\Local Folders\\Inbox";

            }
            else
            {
                path = "Inbox";
            }

            if (File.Exists(path))
            {
                string[] file = File.ReadAllLines(path);
                Console.WriteLine("Reading email SUCCESS!!!");
                return file;
            }
            else
            {
                Console.WriteLine("Error reading file. File do not exist or is not accessible!");
                return null;
            }

        }


        public static List<Data> parseDataOnObject(List<string> _decodedStringList)
        {
            List<Data> result = new List<Data>();

            Data dt = new Data();

            foreach (var q in _decodedStringList)
            {
                //string record = ;

                string[] temp = q.Replace("\r\n", ";").Split(';');

                for (int i = 0; i < temp.Length; i++)
                {
                    if (temp[i] == "NR WEWN SKLEPU")
                    {
                        while (!temp[i + 2].Contains("Przelew"))
                        {
                            dt = new Data();
                            dt.statementDate = temp[i + 1];
                            dt.offerDate = temp[i + 2];
                            dt.shopNumber = temp[i + 5];
                            dt.contractNumber = temp[i + 6].Replace("'", "");
                            dt.creditAmmount = temp[i + 9];
                            dt.credit = temp[i + 9];
                            dt.participation = temp[i + 11];
                            dt.listNumber = temp[i + 13];
                            dt.authorizationNumber = temp[i + 14].Replace("'", "");
                            dt.referenceNumber = temp[i + 15];
                            dt.debit = "";
                            dt.referenceNumber2 = "";
                            dt.clientName = "";
                            i = i + 15;
                            result.Add(dt);
                        }
                        break;
                    }

                }
            }
            Console.WriteLine("Creating objects SUCCESS!!!");
            return result;
        }

        public static void SaveDataToCSV(List<Data> _saveData)
        {
            string filePath = "report.csv";

            if (File.Exists(filePath) == true)
            {
                try
                {
                    File.Delete(filePath);
                }
                catch (Exception)
                {

                    Console.WriteLine("Cannot delete previous report");
                }

            }


            try
            {
                System.IO.StreamWriter file = new System.IO.StreamWriter(filePath);
                file.WriteLine(
                    "NrWykazu;NrSklepu;NrRef.;DataZest;Numer umowy;Nazw. klienta;Nr.Autor.;Nr. Ref.;Dat oferty;Wys kredyt;Udz sklepu;Debet;Kredyt");

                foreach (var q in _saveData)
                {
                    file.WriteLine(q.listNumber + ";" + q.shopNumber + ";" + q.referenceNumber + ";" + q.statementDate +
                                   ";" + q.contractNumber + ";" + q.clientName + ";" +
                                   q.authorizationNumber + ";" + q.referenceNumber2 + ";" + q.offerDate + ";" +
                                   q.creditAmmount + ";" + q.participation + ";" + q.debit + ";" + q.credit);
                }
                Console.WriteLine("Creating report SUCCESS!!!");
                file.Close();
            }
            catch (Exception)
            {

                Console.WriteLine("Cannot create report, unhandled error");
            }

        }
    }
}
